<?php
namespace Home\Controller;

use Home\Model\AdminUserModel;
use Think\Controller;
use Common\Controller\NBaseController;

class LoginController extends NBaseController
{
    public function index(){
        $this->signin();
    }

    /**
     * 登陆页面
     */
    public function signin()
    {
//        if (!empty($this->user)) {
//            //如果已登录,跳转至首页
//            $this->redirect("/");
//        }
        $from = $this->params['from'] ? : '/';
        $this->assign('from', $from);
        layout("login");
        $this->display('login');
    }

    /**
     * 找回密码
     */
    public function findpassp()
    {
        layout("login");
        $this->display("findpass");
    }

    /**
     * 处理ajax请求
     */
    public function ajax()
    {
        $action = $this->params['action'];

        switch ($action) {
            case "login_admin" :
                $this->loginHandle();
                exit;
                break;
            default :

                break;
        }
    }

    /**
     * 处理登录逻辑
     */
    private function loginHandle()
    {
        if (empty($this->params['username'])
            || empty($this->params['password'])
        ) {
            $this->jsonReturn(10000, "", "参数不齐");
        }

        $adminUserModel = new AdminUserModel();
        $res = $adminUserModel->loginWithUsername($this->params['username'], $this->params['password']);
        if ($res['status']) {
            $json = [
                'status'    => 200
            ];
        } else {
            $json = [
                'status'    => $res['code'],
                'msg'       => $res['msg']
            ];
        }
        $this->ajaxReturn($json);
    }


    public function logout()
    {
        session_unset();
        session_destroy();

        redirect("/login/signin");
    }

}